<template>
	<view class="all">
		<view class="uploadTit">
			上传图片
		</view>
		<view class="addImg">
			<view class="sc2" v-for="(item, index) in imgList" :key="index">
			    <image class="del" @click="del(index)" src="@/static/chacha.svg" mode=""></image>
			    <image class="Img3" :src="item" mode="aspectFit"></image>
			</view>
			<view class="add" v-if="imgList.length < 1" @click="upload"><image class="img" src="@/static/jiahao.svg" mode=""></image></view>
		</view>
		<view><button :disabled="subBtn"  class="btn" type="primary" @click="submitData">上传</button></view>
		
		<view class="result" v-show="Object.keys(detectResult).length!==0">
			<view class="uploadTit">
				识别结果
			</view>
			<image class="resultImg" :src="getImgPath(resulltImg.n_path)" mode="aspectFit"></image>
			<view class="resultText" v-for="item, ind in detectResult" :key="ind">
				<view>检测：{{item.label}}， 置信度：{{item.conf}}</view>
				<view>分类：{{item.details}} </view>
			</view>
			<view><button :disabled="accBtn"  class="btn" type="primary" @click="submitAcc">预测正确</button></view>
			<view class="tips">告诉作者预测是否正确，为作者提供更多数据来提高预测的准确度</view>
		</view>
		
	</view>
</template>

<script>
	import {uploadFile, request} from"@/api/request.js"
	export default {
		data() {
			return {
				imgList:[],
				imgResIdList:[],
				detectResult:[],
				resulltImg:{},
				accBtn: true,
				subBtn: false,
			}
		},
		methods: {
			upload() {
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album'], //从相册选择
						loop: true,
						success: res => {
							// console.log(res);
							if (res.tempFilePaths.length != 0) {
								this.imgList.push(res.tempFilePaths[0]);
								
							}
						}
					});
					
					// console.log()
				},
				
				
				// 删除图片
				del(index) {
					this.imgList.splice(index, 1);
					// console.log(this.imgList);
					this.subBtn = false
				},
				
				submitData(){
					if(this.imgList.length>0){
						uni.showLoading({
						                    title: '识别中'
						                });
						this.addImg()
						this.accBtn = false
						this.subBtn = true
						
					}
					else{
						uni.showToast({
							title: '请选择图片',
							duration: 2000,
							icon:'error'
						});
					}
				},
				addImg(){
					var data = {
						url: '/UploadImg',
						method:'POST',
						// formData:{name:'img'},
						filePath:this.imgList[0]
					}
					uploadFile(data).then(res=>{
						// console.log(res, typeof(res))
						// console.log(res)
						let resData = JSON.parse(res)
						if (resData.code == 1){
							// console.log(resData)
							this.detectResult = resData.data
							this.resulltImg = resData.img_data
							
							// console.log(this.imgResIdList)
						}
						uni.hideLoading();
						
						
					})
				
				},
				getImgPath(path){
					// console.log(config)
					return config.imgurl + path
				},
				
				
				
				submitAcc(){
					var res = request({
						url: '/UploadImg',
						method:'PUT',
						data:{id: this.resulltImg.id}
					}).then(res =>{
						// console.log(res)
						if (res.code == 1){
							// console.log(res)
							uni.showToast({
								title: '提交成功',
								duration: 2000,
								icon:'success'
							});
						}
					})
				this.accBtn = true
				}
		}
	}
</script>

<style  lang="scss">

	.uploadTit{
		margin: 20rpx 0rpx;
		text-align: center;
	}
	.addImg{
		
		width: 400rpx;
		height: 400rpx;
		align-items: center;
		margin-left: auto;
		margin-right: auto;
		margin-top: 30rpx;
		.img{
			width: 400rpx;
			height: 400rpx;
		}
	}
	
	.sc2 {
		width: 400rpx;
		height: 400rpx;
		border-radius: 10rpx;
		// background-color: #dadfef;
		text-align: center;
		line-height: 240rpx;
		// margin: 0 10rpx;
		position: relative;
		// background-color: #4CD964;
	}
	.Img3 {
		width: 100%;
		height: 100%;
		border-radius: 10rpx;
	}
	.del {
		width: 32rpx;
		height: 32rpx;
		position: absolute;
		z-index: 1000;
		top: 0rpx;
		right: 0;
	}
	.add{
		border: 1rpx solid grey;
	}
	.btn{
		width: 80%;
		margin-top: 50rpx;
	}
	.result{
		background-color: ghostwhite;
		width: 600rpx;
		// height: 400rpx;
		align-items: center;
		margin-left: auto;
		margin-right: auto;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		.resultImg{
			width: 600rpx;
			height: 400rpx;
			// height: 100%;
		}
	}
	.tips{
		margin-top: 10rpx;
		font-size: 25rpx;
	}
</style>
